package com.lee.pullrefresh.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class VHAdapter extends BaseAdapter {	
	
	protected abstract class VH {
		public abstract void setupViewItem(View v, int position);
		public abstract void fillViewItem(Object obj, int position);
	}

	protected Activity mContext;
	
	protected ArrayList<Object> mItems;
	
	protected LayoutInflater mInflater;
	
	public VHAdapter(Activity c) {
		mContext = c;
		mInflater = LayoutInflater.from(c);
	}
	
	public void setItems(ArrayList<Object> list){
		if (mItems != null) {
			mItems.clear();
			mItems = null;
		}
		mItems = (ArrayList<Object>)list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (mItems != null)
			return mItems.size();
		return 0;
	}

	@Override
	public Object getItem(int paramInt) {
		if ((mItems != null) && (mItems.size() > 0) && (paramInt >= 0)
				&& (paramInt < mItems.size()))
			return mItems.get(paramInt);
		else
			return null;
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	public void removeAll() {
		if (mItems != null) {
			mItems.clear();
			notifyDataSetChanged();
		}
	}

	public void addNoNotifyUI(Object i) {
		if (mItems != null) {
			mItems.add(i);
		} else {
			mItems = new ArrayList<Object>();
			mItems.add(i);
		}
	}
	
	public void replace(int index,Object i) {
		if ((mItems != null) && (mItems.size() > 0) && (index >= 0)
				&& (index < mItems.size())) {
			mItems.set(index, i);
		}
	}


	public void add(Object i) {
		if (mItems != null) {
			mItems.add(i);
		} else {
			mItems = new ArrayList<Object>();
			mItems.add(i);
		}
		notifyDataSetChanged();
	}

	public void insert(int index, Object i) {
		if (index < 0)
			index = 0;
		if (mItems != null) {
			mItems.add(index, i);
		} else {
			mItems = new ArrayList<Object>();
			mItems.add(i);
		}
		notifyDataSetChanged();
	}

	public void insertNoNotifyUI(int index, Object i) {
		if (mItems != null) {
			mItems.add(index, i);
		} else {
			mItems = new ArrayList<Object>();
			mItems.add(i);
		}
	}

	public void update(Object from, Object to) {
		if (mItems != null) {
			int i = mItems.indexOf(from);
			if (i >= 0 && i < mItems.size()) {
				mItems.remove(from);
				mItems.add(i, to);
			}
			notifyDataSetChanged();
		}
	}

	public void remove(Object o) {
		if (mItems != null && o != null) {
			mItems.remove(o);
			notifyDataSetChanged();
		}
	}
	
	public void remove(int paramInt) {
		if (mItems != null && mItems.size() > 0 && paramInt >= 0
				&& paramInt < mItems.size()) {
			mItems.remove(paramInt);
			notifyDataSetChanged();
		}
	}
	
	public ArrayList<Object> items() {
		return mItems;
	}
	
	protected abstract View createItemView(int position);

	protected abstract VH createViewHolder(int position);
	
	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		VH vh = null;
		if (convertView == null) {
			vh = createViewHolder(position);
			convertView = createItemView(position);
			vh.setupViewItem(convertView, position);
			convertView.setTag(vh);
		} else {
			vh = (VH) convertView.getTag();
		}
		if (vh != null) {
			vh.fillViewItem(getItem(position), position);
		}
		return convertView;
	}
}